#include "window.h"
#include "ui_window.h"

#include <QMenu>
#include <QMessageBox>
#include <QCloseEvent>
#include <QComboBox>
#include <QTextLine>
#include <QStandardItemModel>
#include <QDebug>
#include <QDateTime>

window::window(QWidget *parent)
        : QDialog(parent), ui(new Ui::window) {
    ui->setupUi(this);
    createActions();
    createTrayIcon();
    trayIcon->show();

//    showMessage(1, "hello world");

    model = new QStandardItemModel(this);
    model->setColumnCount(4);
    model->setHeaderData(0, Qt::Horizontal, QString::fromUtf8("Time"));
    model->setHeaderData(1, Qt::Horizontal, QString::fromUtf8("Type"));
    model->setHeaderData(2, Qt::Horizontal, QString::fromUtf8("Scope"));
    model->setHeaderData(3, Qt::Horizontal, QString::fromUtf8("Message"));
    ui->tableView->setModel(model);
    ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft);

    // addTableItem(QString::fromUtf8("haha"));
}

window::~window() {
    delete ui;
}

void window::setVisible(bool visible) {
    restoreAction->setEnabled(isMaximized() || !visible);
    QDialog::setVisible(visible);
}

void window::showMessage(int level, QString msg) {
    QSystemTrayIcon::MessageIcon msgIcon;
    QString strTitle;
    switch (level) {
        case info:
            msgIcon = QSystemTrayIcon::MessageIcon::Information;
            strTitle = QString("build success");
            break;
        case warning:
            msgIcon = QSystemTrayIcon::MessageIcon::Warning;
            break;
        case error:
            msgIcon = QSystemTrayIcon::MessageIcon::Critical;
            strTitle = QString("build fail");
            break;
        default:
            msgIcon = QSystemTrayIcon::MessageIcon::Information;
    }

    trayIcon->showMessage(strTitle, msg, msgIcon);
}

void window::closeEvent(QCloseEvent *event) {
#ifdef Q_OS_OSX
    if (!event->spontaneous() || !isVisible()) {
        return;
    }
#endif
    if (trayIcon->isVisible()) {
       // QMessageBox::information(this, tr("Systray"),
       //                          tr("The program will keep running in the "
       //                             "system tray. To terminate the program, "
       //                             "choose <b>Quit</b> in the context menu "
       //                             "of the system tray entry."));
        hide();
        event->ignore();
    }
}

void window::createActions() {
    restoreAction = new QAction(tr("&Restore"), this);
    connect(restoreAction, &QAction::triggered, this, &QWidget::showNormal);

    quitAction = new QAction(tr("&Quit"), this);
    connect(quitAction, &QAction::triggered, qApp, &QCoreApplication::quit);
}

void window::createTrayIcon() {
    trayIconMenu = new QMenu(this);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setContextMenu(trayIconMenu);

    QIcon icon = QIcon(":/images/heart.png");
    trayIcon->setIcon(icon);

    connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), this, SLOT(doubleClickShowWindow(QSystemTrayIcon::ActivationReason)));
}

void window::addTableItem(int level, QString sendAddr, QString msg) {
    qDebug() << model->rowCount();
    int row = model->rowCount();
    model->setItem(row, 0, new QStandardItem(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")));
    if (sendAddr == "10.30.30.200") {
        model->setItem(row, 2, new QStandardItem(QString::fromLocal8Bit("Prod")));
        // msg.insert(0, QString::fromLocal8Bit("正式环境 "));
    } else {
        model->setItem(row, 2, new QStandardItem(QString::fromLocal8Bit("Test")));
        // msg.insert(0, QString::fromLocal8Bit("测试环境 "));
    }
    model->setItem(row, 3, new QStandardItem(msg));
    switch (level) {
        case info:
            model->setItem(row, 1, new QStandardItem("success"));
            for (int i = 0; i< model->columnCount(); i++) {
                model->item(row, i)->setForeground(QBrush(QColor(0,139,0))); // 绿色
            }
            break;
        case warning:
            model->setItem(row, 1, new QStandardItem("warning"));
            for (int i = 0; i< model->columnCount(); i++) {
                model->item(row, i)->setForeground(QBrush(QColor(218,165,3))); // 黄色
            }
            break;
        case error:
            model->setItem(row, 1, new QStandardItem("failure"));
            for (int i = 0; i< model->columnCount(); i++) {
                model->item(row, i)->setForeground(QBrush(QColor(255, 0, 0))); // 红色
            }
            break;
        default:
            model->setItem(row, 1, new QStandardItem("other"));
    }

    for (int i = 0; i< model->columnCount(); i++) {
        ui->tableView->resizeColumnToContents(i);
    }
}

void window::doubleClickShowWindow(QSystemTrayIcon::ActivationReason activationReason) {
    if (activationReason == QSystemTrayIcon::ActivationReason::DoubleClick) {
        this->show();
    }
}

